package com.gsafety.library.LifeCycle;

import android.app.Activity;
import android.app.Application;
import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.gsafety.library.Utils.Log;

import java.util.LinkedList;
import java.util.List;

public class ActivityLifeCycle implements Application.ActivityLifecycleCallbacks
{
    private List<Activity> activitys = new LinkedList<>();
    private String KEY = "ActivityLifeCycle";

    @Override
    public void onActivityCreated(@NonNull Activity activity, @Nullable Bundle bundle)
    {
        Log.e(KEY, "%s:onCreate", activity.getClass().getName());
        activitys.add(activity);
    }

    @Override
    public void onActivityStarted(@NonNull Activity activity)
    {
        Log.e(KEY, "%s:onStart", activity.getClass().getName());
    }

    @Override
    public void onActivityResumed(@NonNull Activity activity)
    {
        Log.e(KEY, "%s:onResume", activity.getClass().getName());
    }

    @Override
    public void onActivityPaused(@NonNull Activity activity)
    {
        Log.e(KEY, "%s:onPause", activity.getClass().getName());
    }

    @Override
    public void onActivityStopped(@NonNull Activity activity)
    {
        Log.e(KEY, "%s:onStop", activity.getClass().getName());
    }

    @Override
    public void onActivitySaveInstanceState(@NonNull Activity activity, @NonNull Bundle bundle)
    {
        Log.e(KEY, "%s:onSaveInstance", activity.getClass().getName());
    }

    @Override
    public void onActivityDestroyed(@NonNull Activity activity)
    {
        Log.e(KEY, "%s:onDestroy", activity.getClass().getName());
        activitys.remove(activity);
    }

    public void finishAll()
    {
        for (int i = 0; i < activitys.size(); i++)
        {
            Activity activity = activitys.get(i);
            if (!activity.isFinishing())
            {
                activity.finish();
            }
        }
    }

    public void exit()
    {
        finishAll();
        android.os.Process.killProcess(android.os.Process.myPid());
    }

}
